package cn.kiliter.serviece;

import cn.kiliter.dao.OrderDao;
import cn.kiliter.dao.OrderItemDao;
import cn.kiliter.dao.ProductDao;
import cn.kiliter.excation.UserException;
import cn.kiliter.model.Order;
import cn.kiliter.model.OrderItem;
import cn.kiliter.utils.ManagerThreadLocal;

import java.sql.SQLException;
import java.util.List;

public class OrderService {

    OrderDao orderDao = new OrderDao();
    OrderItemDao orderItemDao = new OrderItemDao();
    ProductDao productDao = new ProductDao();
    public void addOrder(Order order) {
        try {
            ManagerThreadLocal.beginTransaction();
            orderDao.saveOrder(order);
            orderItemDao.saveOrderItem(order.getItems());
            for(OrderItem od:order.getItems()){

                productDao.saveorder(od.getProduct().getId(),od.getBuynum());
            }
         ManagerThreadLocal.commitTransaction();

        } catch (SQLException e) {
            e.printStackTrace();
            ManagerThreadLocal.rollbackTransaction();
        }
    }

    public List<Order> findOrderByID(String s) {
        try {
            return orderDao.findOrderById(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Order findOrderInfoByID(String orderid) {
        try {
            return orderDao.findOrderInfoById(orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updatepaystate(String id) throws UserException {
        try {
            orderDao.updatepaystate(id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new UserException("cuowu");
        }
    }
}
